/**
 * 一些公共的Api  注意各个js的加载顺序
 **/
/**
 * 初始化快递公司列表
 */
function initExpressCompanyList() {
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "GET",
            url: '/express/query/company/list',
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            success: function (req) {
                if (req.code === '200') {
                    let expressCompanyList = req.data
                    layui.use(['form'], function () {
                        var form = layui.form,
                            $ = layui.$;
                        let options = "";
                        for (const index in expressCompanyList) {
                            const expressCompanyMap = new Map(Object.entries(expressCompanyList[index]));
                            expressCompanyMap.forEach((value, key) => {
                                options += "<option value='" + key + "'>" + value + "</option>";
                            });
                        }
                        $("select[name='expressCompanyCode']").append(options);// 重新渲染下拉框
                        form.render('select');
                    });
                    resolve(true)
                } else {
                    reject(req.msg)
                }
            },
            error: function (e) {
                layer.msg(e.msg);
            }
        })
    });
}

/**
 * 获取管理员列表
 */
function initExtensionUser(data) {
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "GET",
            url: '/user/all/list',
            data: data,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            success: function (req) {
                if (req.code === '200') {
                    let userList = req.data
                    resolve(userList)
                } else {
                    reject(req.msg)
                }
            },
            error: function (e) {
                layer.msg(e.msg);
                reject(e.msg)
            }
        })
    });
}

/**
 * 获取商品供应链列表
 */
function getSupplyChainList(parentId){
    if (!isNotNull(parentId)){
        return ;
    }
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "GET",
            url: `/goods/supply/chain/list/${parentId}`,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            success: function (req) {
                if (req.code === '200') {
                    let list = req.data
                    resolve(list)
                } else {
                    reject(req.msg)
                }
            },
            error: function (e) {
                layer.msg(e.msg);
                reject(e.msg)
            }
        })
    });
}

/**
 * 获取商品供应链详情
 */
function getSupplyChainInfo(id){
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "GET",
            url: `/goods/supply/chain/info/${id}`,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            success: function (req) {
                if (req.code === '200') {
                    let data = req.data
                    resolve(data)
                } else {
                    reject(req.msg)
                }
            },
            error: function (e) {
                layer.msg(e.msg);
                reject(e.msg)
            }
        })
    });
}

/**
 * 获取全部企业名单
 */
function getEnterpriseList(){
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "GET",
            url: `/enterprise/all/list`,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            success: function (req) {
                if (req.code === '200') {
                    let list = req.data
                    resolve(list)
                } else {
                    reject(req.msg)
                }
            },
            error: function (e) {
                layer.msg(e.msg);
                reject(e.msg)
            }
        })
    });
}

/**
 * 获取课程分类列表
 */
function getCourseCategoryList(parentId){
    if (!isNotNull(parentId)){
        return ;
    }
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "GET",
            url: `/course/category/list/${parentId}`,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            success: function (req) {
                if (req.code === '200') {
                    let list = req.data
                    resolve(list)
                } else {
                    reject(req.msg)
                }
            },
            error: function (e) {
                layer.msg(e.msg);
                reject(e.msg)
            }
        })
    });
}

/**
 * 获取课程分类详情
 */
function getCourseCategoryInfo(id){
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "GET",
            url: `/course/category/info/${id}`,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            success: function (req) {
                if (req.code === '200') {
                    let data = req.data
                    resolve(data)
                } else {
                    reject(req.msg)
                }
            },
            error: function (e) {
                layer.msg(e.msg);
                reject(e.msg)
            }
        })
    });
}